package com.shopping.dev.service.impl;

import com.shopping.dev.bo.SubmitOrderBO;
import com.shopping.dev.common.enums.OrderStatusEnum;
import com.shopping.dev.common.enums.YesOrNoEnum;
import com.shopping.dev.common.util.IdUtil;
import com.shopping.dev.mapper.OrderItemsMapper;
import com.shopping.dev.mapper.OrderStatusMapper;
import com.shopping.dev.mapper.OrdersMapper;
import com.shopping.dev.pojo.*;
import com.shopping.dev.service.AddressService;
import com.shopping.dev.service.ItemsService;
import com.shopping.dev.service.OrderService;
import com.shopping.dev.vo.MerchantOrdersVO;
import com.shopping.dev.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private AddressService addressService;

    @Autowired
    private OrderItemsMapper orderItemsMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrderStatusMapper orderStatusMapper;
    @Autowired
    private ItemsService itemsService;
    @Override
    public OrderVO createOrder(SubmitOrderBO submitOrderBO) {
        //1.获取地址信息
        UserAddress userAddress = addressService.queryUserAddress(submitOrderBO.getUserId(), submitOrderBO.getAddressId());
        //2.组织订单基础数据
        String orderId = IdUtil.getUUID();
        Orders orders = new Orders();
        orders.setId(orderId);
        orders.setUserId(submitOrderBO.getUserId());
        orders.setReceiverName(userAddress.getReceiver());
        orders.setReceiverMobile(userAddress.getMobile());
        orders.setReceiverAddress(userAddress.getProvince()+" "+userAddress.getCity()+" " +userAddress.getDistrict()+" "+userAddress.getDetail());
        orders.setPostAmount(0);
        orders.setPayMethod(submitOrderBO.getPayMethod());
        orders.setLeftMsg(submitOrderBO.getLeftMsg());
        orders.setIsComment(YesOrNoEnum.NO.getCode());
        orders.setIsDelete(YesOrNoEnum.NO.getCode());
        orders.setCreatedTime(new Date());
        orders.setUpdatedTime(new Date());
        String[] itemspecids = submitOrderBO.getItemSpecIds().split(",");
        Integer totalAmount = 0;//订单总额
        Integer realPayAmont = 0;//优惠后实际支付金额
        //3.获取每个商品信息，查询价格，累计金额
        Integer buyCounts = 1;
        for (String specid:itemspecids){
            ItemsSpec itemsSpec = itemsService.queryItemsSpecById(specid);
            totalAmount+=itemsSpec.getPriceNormal();
            realPayAmont+=itemsSpec.getPriceDiscount();
            //4.获取订单每个商品信息，保存到订单商品表
            itemsService.queryItemById(itemsSpec.getItemId());
            String imgurl = itemsService.queryItemMainImgById(itemsSpec.getItemId());
            OrderItems orderItems = new OrderItems();
            orderItems.setId(IdUtil.getUUID());
            orderItems.setOrderId(orderId);
            orderItems.setItemId(itemsSpec.getItemId());
            orderItems.setItemName(itemsSpec.getName());
            orderItems.setItemImg(imgurl);
            orderItems.setBuyCounts(buyCounts);
            orderItems.setItemSpecId(itemsSpec.getId());
            orderItems.setItemSpecName(itemsSpec.getName());
            orderItems.setPrice(itemsSpec.getPriceDiscount());
            orderItemsMapper.insert(orderItems);
            //5.扣减库存
            itemsService.decreaseItemSpecStock(specid,buyCounts);
        }
        //6.保存订单状态表
        orders.setTotalAmount(totalAmount);
        orders.setRealPayAmount(realPayAmont);
        ordersMapper.insert(orders);

        OrderStatus os = new OrderStatus();
        os.setOrderId(orderId);
        os.setOrderStatus(OrderStatusEnum.WAIT_PAY.getType());
        os.setCreatedTime(new Date());
        orderStatusMapper.insert(os);
        //7.组织支付信息和补充返回信息
        MerchantOrdersVO merchantOrdersVO = new MerchantOrdersVO();
        merchantOrdersVO.setMerchantOrderId(orderId);
        merchantOrdersVO.setMerchantUserId(submitOrderBO.getUserId());
        merchantOrdersVO.setAmount(realPayAmont+ orders.getPostAmount());
        merchantOrdersVO.setPayMethod(submitOrderBO.getPayMethod());
        OrderVO orderVO = new OrderVO();
        orderVO.setOrderId(orderId);
        orderVO.setMerchantOrdersVO(merchantOrdersVO);
        return orderVO;
    }
}
